package com.ssyt.ExaminationSystem.admin.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ssyt.ExaminationSystem.admin.entity.PermissionEntity;
import com.ssyt.ExaminationSystem.admin.entity.RoleEntity;
import com.ssyt.ExaminationSystem.admin.service.IsystemService;
import com.ssyt.ExaminationSystem.admin.service.SystemServiceImpl;
import com.ssyt.ExaminationSystem.student.entity.GradeEntity;

/**
 * Servlet implementation class PermissionServlet
 */
public class PermissionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String flag = request.getParameter("flag");

		if ("role".equals(flag)) {
			initRoles(request, response);

		} else if ("initPermission".equals(flag)) {
			initPermission(request, response);

		} else if ("grant".equals(flag)) {
			showGrant(request, response);

		} else if ("gotoGrant".equals(flag)) {
			gotoGrant(request, response);

		} else if ("getGrant".equals(flag)) {
			getGrant(request, response);

		} else if ("doGrant".equals(flag)) {
			doGrant(request, response);

		} else if ("addRole".equals(flag)) {
			addRole(request, response);

		} else if ("preUpdate".equals(flag)) {
			preUpdate(request, response);

		} else if ("deleteRole".equals(flag)) {
			deleteRole(request, response);

		} else if ("classManager".equals(flag)) {
			classManager(request, response);

		} else if ("deleteGrade".equals(flag)) {
			deleteGrade(request, response);

		} else if ("preUpdateGrade".equals(flag)) {
			preUpdateGrade(request, response);

		} else if ("updateGrade".equals(flag)) {
			updateGrade(request, response);

		} else if ("addPermission".equals(flag)) {
			addPermission(request, response);

		} else if ("grantManager".equals(flag)) {
			grantManager(request, response);

		} else if ("insertGrant".equals(flag)) {
			insertGrant(request, response);

		} else if ("grantControl".equals(flag)) {
			grantControl(request, response);

		} else if ("deleteGrant".equals(flag)) {
			deleteGrant(request, response);

		} else if ("preUpdateGrant".equals(flag)) {
			preUpdateGrant(request, response);

		} else if ("updateGrant".equals(flag)) {
			updateGrant(request, response);

		} else {
			System.out.println("没有匹配的flag");
		}

	}

	/**
	 * 初始化所有的角色
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initRoles(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		List<RoleEntity> roleList = systemService.initRoles();

		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("pages/role.jsp").forward(request,
				response);

	}

	/**
	 * 初始化当前用户的权限菜单
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void initPermission(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		PrintWriter out = response.getWriter();
		out.print(systemService.getPermissionJsonByaccount("admin"));
		out.flush();
		out.close();

	}

	/**
	 * 展示所有的权限
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void showGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		IsystemService systemService = new SystemServiceImpl();
		String grants = systemService.getPermissions();
		PrintWriter out = response.getWriter();
		out.print(grants);
		out.flush();
		out.close();

	}

	/**
	 * 点击授权进入授权界面
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void gotoGrant(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String roleId = request.getParameter("roleId");
		request.getRequestDispatcher("pages/grant.jsp?roleId=" + roleId)
				.forward(request, response);
	}

	/**
	 * 根据角色编号获取所拥有的权限--json字符串
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void getGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String roleId = request.getParameter("roleId");
		IsystemService systemService = new SystemServiceImpl();
		String grants = systemService.getGrantsByRoleId(roleId);
		PrintWriter out = response.getWriter();
		out.print(grants);
		out.flush();
		out.close();
	}

	/**
	 * 正真的授权
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void doGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		IsystemService systemService = new SystemServiceImpl();
		String permissionsId = request.getParameter("permissionsId");
		String roleId = request.getParameter("roleId");

		boolean mark = systemService.isDoGrant(permissionsId, roleId);
		PrintWriter out = response.getWriter();

		if (mark) {
			// 授权成功，回到登录页面
			// out.print("<script>alert('授权成功');top.location.href='pages/login.jsp';</script>");
			out.print("<script>alert('授权成功');location.href='pages/login.jsp';</script>");
		} else {
			System.out.println("授权失败");
		}

	}

	/**
	 * 角色新增
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void addRole(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String roleName = request.getParameter("roleName");
		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.addRole(roleName);
		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=role");
		} else {
			response.sendRedirect("pages/addRoles.jsp");
		}

	}

	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdate(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		List<RoleEntity> roleList = systemService.initRoles();

		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("pages/addUpdate.jsp").forward(request,
				response);

	}

	/**
	 * 删除角色
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void deleteRole(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String roleId = request.getParameter("roleId");
		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.deleteRole(roleId);
		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=preUpdate");
		} else {
			response.sendRedirect("pages/addUpdate.jsp");
		}

	}

	/**
	 * 班级管理
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void classManager(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		List<GradeEntity> grades = systemService.classManager();

		request.setAttribute("grades", grades);
		request.getRequestDispatcher("pages/classManager.jsp").forward(request,
				response);

	}

	/**
	 * 删除班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void deleteGrade(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		IsystemService systemService = new SystemServiceImpl();
		String gradeId = request.getParameter("gradeId");
		int row = systemService.deleteGrade(gradeId);
		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=classManager");
		} else {
			response.sendRedirect("PermissionServlet?flag=classManager");
		}
	}

	/**
	 * 修改班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdateGrade(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String gradeId = request.getParameter("gradeId");
		IsystemService systemService = new SystemServiceImpl();
		GradeEntity grade = systemService.preUpdateGrade(gradeId);
		request.setAttribute("grade", grade);
		if (grade != null) {
			request.getRequestDispatcher("pages/gradeUpdate.jsp").forward(
					request, response);
		} else {
			response.sendRedirect("pages/classManager.jsp");
		}
	}

	/**
	 * 修改班级
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void updateGrade(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String gradeId = request.getParameter("gradeId");
		String gradeName = request.getParameter("gradeName");

		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.updateGrade(gradeId, gradeName);

		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=classManager");
		} else {
			response.sendRedirect("pages/classManager.jsp");
		}

	}

	/**
	 * 查询所有的父权限
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addPermission(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		List<PermissionEntity> list = systemService.getFatherGrant();
		request.setAttribute("list", list);

		request.getRequestDispatcher("pages/addPermission.jsp").forward(
				request, response);

	}

	/**
	 * 根据父权限id，通过ajax查询子权限id
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void grantManager(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String fatherId = request.getParameter("permissionId");
		IsystemService systemService = new SystemServiceImpl();
		String sonStr = systemService.getSonGrantsById(fatherId);
		PrintWriter out = response.getWriter();
		out.print(sonStr);

		out.flush();
		out.close();

	}

	/**
	 * 从已有的付权限下增加子权限
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void insertGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String fatherId = request.getParameter("fatherPerId");
		String sonId = request.getParameter("sonPerId");
		String sonName = request.getParameter("sonName");
		String sonUrl = request.getParameter("sonUrl");

		PermissionEntity permission = new PermissionEntity();
		permission.setParentId(fatherId);
		permission.setPermissionId(sonId);
		permission.setPermissionName(sonName);
		permission.setPermissionUrl(sonUrl);

		System.out.println(permission);

		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.addPermissions(permission);

		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=grantControl");
		} else {
			request.setAttribute("permission", permission);
			request.getRequestDispatcher("pages/addPermission.jsp").forward(
					request, response);
		}

	}

	/**
	 * 权限管理
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void grantControl(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		IsystemService systemService = new SystemServiceImpl();
		List<PermissionEntity> allGrantList = systemService.getAllGrants();
		request.setAttribute("allGrantList", allGrantList);

		request.getRequestDispatcher("pages/grantManager.jsp").forward(request,
				response);

	}

	/**
	 * 权限删除
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void deleteGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		String permissionId = request.getParameter("permissionId");
		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.deleteGrant(permissionId);

		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=grantControl");
		} else {
			response.sendRedirect("pages/grantManager.jsp");
		}

	}

	/**
	 * 权限修改前的查询
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void preUpdateGrant(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String permissionId = request.getParameter("permissionId");
		IsystemService systemService = new SystemServiceImpl();
		PermissionEntity permission = systemService
				.preUpdateGrant(permissionId);

		request.setAttribute("permission", permission);
		request.getRequestDispatcher("pages/preUpdateGrant.jsp").forward(
				request, response);

	}

	/**
	 * 权限修改
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	private void updateGrant(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String permissionId = request.getParameter("pid");
		String fatherId = request.getParameter("pfid");
		String pname = request.getParameter("pname");
		String url = request.getParameter("url");

		PermissionEntity per = new PermissionEntity();
		per.setPermissionId(permissionId);
		per.setParentId(fatherId);
		per.setPermissionName(pname);
		per.setPermissionUrl(url);

		IsystemService systemService = new SystemServiceImpl();
		int row = systemService.updateGrant(per);
		System.out.println(row);

		if (row == 1) {
			response.sendRedirect("PermissionServlet?flag=grantControl");
		} else {
			response.sendRedirect("pages/grantManager.jsp");
		}

	}

}
